package org.ens.delegation.dao.impl;

import java.util.List;
import org.ens.delegation.bo.Etablissement;
import org.ens.delegation.dao.IEtablissementDao;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

public class EtablissementDao extends GenericDao<Etablissement, String> implements IEtablissementDao{

    @Override
    public Class<Etablissement> getReferencedClass() {
            return Etablissement.class;
    }

    @Override
    public List<Etablissement> findByFilters(String orderCol, String orderDir, String nomAr, String nomFr, String idCommune, String idTypeEtablissement) {
        
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAr", nomAr));
        
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFr", nomFr));
        
        if(idCommune != null && !idCommune.isEmpty())
            criteria.add(Restrictions.eq("commune.id", new Integer(idCommune)));
        
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public List<Etablissement> findByFiltersWithFirstMaxResult(int first, int max, String orderCol, String orderDir, String nomAr, String nomFr, String idCommune, String idTypeEtablissement) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        Order o;
        if(orderDir.equals("asc"))
            o = Order.asc(orderCol);
        else o = Order.desc(orderCol);
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAr", nomAr));
        
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFr", nomFr));
        
        if(idCommune != null && !idCommune.isEmpty())
            criteria.add(Restrictions.eq("commune.id", new Integer(idCommune)));
        
        criteria.setFirstResult(first);
        criteria.setMaxResults(max);
        criteria.addOrder(o);
        return criteria.list();
    }

    @Override
    public Integer getRowCountByFilters(String nomAr, String nomFr, String idCommune, String idTypeEtablissement) {
                
        Criteria criteria = getHibernateTemplate().getSessionFactory().getCurrentSession().createCriteria(getReferencedClass());
        
        if(nomAr != null && !nomAr.isEmpty())
            criteria.add(Restrictions.like("nomAr", nomAr));
        
        if(nomFr != null && !nomFr.isEmpty())
            criteria.add(Restrictions.like("nomFr", nomFr));
        
        if(idCommune != null && !idCommune.isEmpty())
            criteria.add(Restrictions.eq("commune.id", new Integer(idCommune)));
        
        return (Integer) criteria.uniqueResult();
    }

}
